﻿using SimpleInjector;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.Entities;
using Sustainalytics.MongoDB.Extensions;
using Sustainalytics.Replication.Publisher;
using System.Collections.Generic;
using System.Configuration.Abstractions;
using System.Web.Http;

namespace Sustainalytics.MostRecentActivity.Service
{
    public static class CompositionRoot
    {
        public static Container RegisterDependencies(HttpConfiguration config)
        {
            var container = new Container();

            container.Options.DefaultScopedLifestyle = new SimpleInjector.Lifestyles.AsyncScopedLifestyle();
            container.RegisterWebApiControllers(config);

            container.RegisterCollections();

            var serviceBusParams = new Dictionary<string, string>
                {
                        { "ConnectionString", ConfigurationManager.Instance.AppSettings["Microsoft.ServiceBus.ConnectionString"] },
                        { "RegionEndpoint", ConfigurationManager.Instance.AppSettings["Microsoft.ServiceBus.RegionEndpoint"] },
                        { "SessionId", ConfigurationManager.Instance.AppSettings["Microsoft.ServiceBus.SessionId"] },
                        { "TopicPath", ConfigurationManager.Instance.AppSettings["Microsoft.ServiceBus.TopicPath"] },
                };

            container.RegisterMisc(serviceBusParams);

            container.Verify();
            return container;

        }

        public static void RegisterCollections(this Container container)
        {

            container.Register(() => MongoFactory.GetConnectionStrings());
            container.Register(() => MongoFactory.GetCollectionFromClientPlatform<Entities.MostRecentActivity>());
            container.Register(() => MongoFactory.GetCollectionFromDatabase<CorporateData>(db => db.ClientDW));
        }

        public static void RegisterMisc(this Container container, Dictionary<string, string> serviceBusParams)
        {
            container.Register(() => new ServiceBusPublisher(serviceBusParams), Lifestyle.Singleton);
        }
    }
}
